package com.hr.mes.controller;

import com.hr.mes.dto.Page;
import com.hr.mes.dto.Result;
import com.hr.mes.entity.Release;
import com.hr.mes.enums.MyExceptionEnum;
import com.hr.mes.exception.MyException;
import com.hr.mes.service.ReleaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "更新日志")
@CrossOrigin
@RequestMapping("/api/release")
@RestController
public class ReleaseController {

    @Autowired
    private ReleaseService releaseService;

    @RequiresRoles("admin")
    @ApiOperation("新增更新日志")
    @PostMapping("addRelease")
    public Result addRelease(@RequestBody Release release){
        try{
            releaseService.addRelease(release);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.RELEASE_ADD_ERROR, e);
        }
    }

    @ApiOperation("获取更新日志")
    @GetMapping("getReleasePage")
    public Result getReleasePage(@RequestParam Integer pageNo, @RequestParam Integer pageSize){
        try{
            Page<Release> releaseList = releaseService.getReleaseList(pageNo, pageSize);
            return Result.ok(releaseList);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.RELEASE_VIEW_ERROR, e);
        }
    }

}
